package com.webchat.ugc.messaegqueue.consumer.redis;

import com.webchat.common.enums.messagequeue.MessageQueueEnum;
import com.webchat.common.service.messagequeue.consumer.AbstractRedisQueueConsumer;
import com.webchat.common.util.JsonUtil;
import com.webchat.domain.vo.request.ChattingRequestVO;
import com.webchat.domain.vo.request.mess.ChatMessageRequestVO;
import com.webchat.ugc.service.ChatMessageService;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy(value = false)
@Slf4j
public class RefreshChattingRedisMQConsumer extends AbstractRedisQueueConsumer<ChatMessageRequestVO> {

    @Autowired
    private ChatMessageService chatMessageService;

//    @PostConstruct
    public void initBean() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                RefreshChattingRedisMQConsumer.this.schedule();
            }
        }).start();
    }

    @Override
    protected ChatMessageRequestVO convert(String s) {

        return JsonUtil.fromJson(s, ChatMessageRequestVO.class);
    }

    @Override
    protected MessageQueueEnum getMessageQueue() {

        return MessageQueueEnum.QUEUE_CHATTING_LIST_REFRESH;
    }

    @Override
    protected void receive(ChatMessageRequestVO data) {
        if (data == null) {
            return;
        }
        chatMessageService.addChattingList(ChattingRequestVO.builder()
                .account(data.getSenderId())
                .chatAccount(data.getReceiverId())
                .lastChatTime(data.getTime() == null ? System.currentTimeMillis() : data.getTime())
                .build());
    }

    @Override
    protected void error(ChatMessageRequestVO data, Exception ex) {

    }
}
